
const Koa = require('koa');
const app = new Koa();
//中间件
const bodyparser = require('koa-bodyparser');

app.use(bodyparser());

app.use(async(ctx) =>{
    //先模拟路由判断,Get => 显示表单,POST =>提交,其他=> 404
    if(ctx.url === '/' && ctx.method ==='GET'){
        //显示表单
        let html = `
                <h1>JSPang Koa2 request Post!</h1>
                <form method = 'POST' action=''>
                    <p>userName</p>
                    <input name="username" /><br/>
                    <p>age</p>
                    <input name="age" /><br/>
                    <p>website</p>
                    <input name="website" /><br/>
                    <button type = "submit">submit</button>
                </form>
        `;

        ctx.body = html;

    }else if (ctx.url === '/' && ctx.method ==='POST'){
        //中间件
        let postdata = ctx.request.body;
        ctx.body = postdata;
    }
    else
    {
        ctx.body = '<h1>404!</h1>';
    }   

});




app.listen(3000,() =>{
     
    console.log('[demo] server is starting at port 3000!');
    
})